package com.jt.shiro;

import com.jt.base.utils.R;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Component;

/** Shiro权限标签 */
@Component
public class ShiroTag {

  /**
   * 是否拥有该权限
   *
   * @param permission 权限标识
   * @return true：是 false：否
   */
  public boolean hasPermission(String permission) {
    Subject subject = SecurityUtils.getSubject();
    return subject != null && subject.isPermitted(permission);
  }

  /**
   * 是否拥有该角色
   * @return true：是 false：否
   */
  public boolean hasRole(String role) {
    Subject subject = SecurityUtils.getSubject();
    return subject != null && subject.hasRole(role);
  }

  public R wrapPermission( R r, String permission){
    r.put("search", hasPermission(permission+":search")?1:0);
    r.put("save", hasPermission(permission+":save")?1:0);
    r.put("updateStatus", hasPermission(permission+":updateStatus")?1:0);
    r.put("delete", hasPermission(permission+":delete")?1:0);
    r.put("import", hasPermission(permission+":import")?1:0);
    r.put("sync", hasPermission(permission+":sync")?1:0);
    r.put("export", hasPermission(permission+":export")?1:0);
    return r;
  }
}
